package at.codebase.web.servlets.admin;

import java.util.ArrayList;
import java.util.List;

import at.codebase.core.log.Log;
import at.codebase.db.dao.EApiPartnerDao;
import at.codebase.db.entities.EApiPartner;
import at.codebase.web.templates.MainTemplate;
import at.codebase.web.util.LocalRequest.RequestInformation;
import at.codebase.web.util.URLParameters.ParameterException;
import at.codebase.web.util.servlet.SecureServlet;
import at.codebase.web.util.template.Template;
import at.codebase.web.util.template.TemplateEngine;

public class PartnerApplications  extends SecureServlet {

	@Override
	protected void initSecurityLevels(ArrayList<UserLevel> secLevels) {
		secLevels.add(UserLevel.ADMIN);
	}

	@Override
	protected void processRequestSecurityLevel(RequestInformation requestInfo, UserLevel level, RequestType requestType) {
		
		if(requestType.equals(RequestType.POST)) {
			if(!tokenCorrect(requestInfo)) {
				processRequestNoAccess(requestInfo, level);
				return;
			}
			
			try {
				String action = requestInfo.getParameters().getString("action");
				System.out.println(action);
				if(action.equals("addPartner")){
					
						String appName = requestInfo.getParameters().getString("appName");
						String appToken = requestInfo.getParameters().getString("appToken");
						String appUrl = requestInfo.getParameters().getString("appUrl");
						String appComment = requestInfo.getParameters().getString("appComment");
						
						if(appName != null && !appName.trim().equals("") &&
								appToken != null && !appToken.trim().equals("") &&
										appUrl != null && !appUrl.trim().equals("")){	
							
							EApiPartner apiPartner = EApiPartner.createPartner(appName, appToken, appUrl, appComment);
	
							if(apiPartner != null){
							
								EApiPartnerDao.saveApiPartner(apiPartner);
								Log.logAdmin(requestInfo.getUser().getId(),
										"Admin (userid="
												+ requestInfo.getUser().getId()
												+ ") added an partner application (partnerid="
												+ apiPartner.getPartnerId() + ")");
							
							}
							
						}
						
				}else if(action.equals("savePartner")){
					
					long partnerId = requestInfo.getParameters().getLong("partnerId");
					String partnerAccessKey = requestInfo.getParameters().getString("partnerKey");
					String partnerUrl = requestInfo.getParameters().getString("partnerUrl");
					
					EApiPartner partner = EApiPartnerDao.getApiPartner(new Long(partnerId));
					if(partner != null && !partnerAccessKey.trim().equals("") && !partnerUrl.trim().equals("")){
						partner.setAccessToken(partnerAccessKey);
						partner.setAppUrl(partnerUrl);
						EApiPartnerDao.saveApiPartner(partner);
						Log.logAdmin(requestInfo.getUser().getId(),
								"Admin (userid="
										+ requestInfo.getUser().getId()
										+ ") changed a partner)");
					}
				}
				/*
				else if(action.equals("removePartner")){
					long partnerId = requestInfo.getParameters().getLong("partnerId");
					EApiPartner partner = EApiPartnerDao.getApiPartner(new Long(partnerId));
					if(partner != null){
						EApiPartnerDao.removePartner(partner);
						Log.logAdmin(requestInfo.getUser().getId(),
								"Admin (userid="
										+ requestInfo.getUser().getId()
										+ ") removed an partner application (name="
										+ partner.getAppName() + ")");
					}
					
				}
				*/
			} catch (ParameterException e) {
				e.printStackTrace();
				
			}
		}		
		
		MainTemplate mainTemplate = new MainTemplate(requestInfo, writer());
		mainTemplate.setTitle("Application API Access");

		Template overviewTemplate = TemplateEngine.getTemplate("admin/site_partner_apps.html");
		String nextToken = nextToken();
		overviewTemplate.setText("randomKey", nextToken);
		overviewTemplate.setTemplate("adminMenuBar", TemplateEngine.getTemplate("admin/admin_menu_bar.html"));
		
		ArrayList<Template> rowTemplates = new ArrayList<Template>();
		List<EApiPartner> apiPartnerList = EApiPartnerDao.getApiPartnerList();
		
		for(EApiPartner apiPartner:apiPartnerList){
			Template template = TemplateEngine.getTemplate("admin/manage/partner_row.html");
			template.setText("partnerid", apiPartner.getPartnerId().toString());
			template.setText("randomKey", nextToken);
			
			template.setText("appname", apiPartner.getAppName());
			template.setText("appurl", apiPartner.getAppUrl());
			template.setText("comment", apiPartner.getComment());
			
			template.setText("token", apiPartner.getAccessToken());

			rowTemplates.add(template);
		}
		overviewTemplate.setTemplates("partnerList", rowTemplates);
		
		mainTemplate.setTemplate("body", overviewTemplate);
		mainTemplate.render(requestInfo.getLocale());

	}

	
	@Override
	protected RequestType requestType() {
		return RequestType.POST_AND_GET;
	}

}
